﻿using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal struct DXFPat
    {
        public BackgroundPattern fls;
        public IcvXF icvBackground;
        public IcvXF icvForeground;

        private ushort icvs
        {
            get { return (ushort) ((byte) icvForeground | ((byte) icvBackground << 7)); }
        }

        public void Read(BinaryReader reader)
        {
#if DEBUG
            //\\ ReadWriteChecker.Check<DXFPat>((t, r) => t.Read(r), (t, w) => t.Write(w), reader,ref this);
#endif
            reader.ReadByte();
            fls = (BackgroundPattern) (reader.ReadByte() << 2);
            ushort u = reader.ReadUInt16();
            icvForeground = (byte) (u & 0x7F);
            icvBackground = (byte) ((u >> 7) & 0x7F);
            Contract.Assert(icvs == u);
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write((byte) 0);
            var v = (byte) fls;
            v = (byte) (v >> 2);
            writer.Write(v);
            writer.Write(icvs);
        }
    }
}